package cn.tedu.travelsystem.base.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlertService {

    @Autowired
    private JavaMailSender mailSender;

    // 发件人邮箱（必须与配置中的 spring.mail.username 一致）
    private static final String FROM_EMAIL = "2407235934@qq.com";

    // 收件人邮箱（可设为你自己、团队、多个收件人等）
    private static final String TO_EMAIL = "2717048649@qq.com";

    public void sendAlert(String title, String message) {
        // 控制台 & 日志
        System.out.println("【邮件预警】" + title + "：" + message);
        log.warn("【系统预警】{} - {}", title, message);

        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(FROM_EMAIL);
            mailMessage.setTo(TO_EMAIL);
            mailMessage.setSubject("【系统预警】" + title);
            mailMessage.setText(message);

            mailSender.send(mailMessage);
            log.info("✅ 邮件告警已发送");
        } catch (Exception e) {
            log.error("❌ 邮件告警发送失败", e);
        }
    }
}
